package com.sie.project.utils;


import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.util.Date;

/**
 * @author linjinde
 */

public class ResponseResult<T> {
    private String status;
    private T data;
    private Date timestamp;

    public ResponseResult(String status, T data, Date timestamp) {
        this.status = status;
        this.data = data;
        this.timestamp = timestamp;
    }

    // 省略 getter 和 setter 方法

    // 可选的静态工厂方法，用于成功的响应结果
    public static <T> ResponseResult<T> success(T data) {
        return new ResponseResult<>("success", data, new Date());
    }

    // 可选的静态工厂方法，用于失败的响应结果
    public static <T> ResponseResult<T> failure(T data) {
        return new ResponseResult<>("failure", data, new Date());
    }

    // 将整个 ResponseResult 对象转换为 JSON 字符串
    public String toJson() {
        ObjectMapper mapper = new ObjectMapper();
        try {
            return mapper.writeValueAsString(this);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
            return null;
        }
    }
}
